package com.company.project.learning.zaluan;

import org.apache.commons.lang3.time.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: zhaoqi
 * @Date: 11/13/2019 1:04 AM
 * @Version 1.0
 * 二分查找
 */

public class BinarySearch {
    public static void main(String[] args) {


        int[] array = {3,8,9,11,21,25,44,66,75,88,97,111};

        int binasysearch = binarySearch(array,25);
        System.out.println(binasysearch);
    }

    private static int binarySearch(int[] array, int i) {
        int low = 0;
        int high = array.length -1;
        while(low < high){
            int middle = (low + high)/2;
            if (i == array[middle]){
                return middle;
            }else if (i < array[middle]){
                high = middle -1;
            }else {
                low = middle +1;
            }

        }
        return -1;
    }
}
